package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.cart.CartFeignClient;
import com.atguigu.gmall.model.cart.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2024/10/5 1:13
 */
@Controller
public class CartController {
    @Autowired
    CartFeignClient cartFeignClient;

    @GetMapping("/cart.html")
    public String cartPage() {
        return "cart/index";
    }

    //addCart.html?skuId=42&skuNum=1
    @GetMapping("/addCart.html")
    public String addCartPage(@RequestParam("skuId") Long skuId,
                              @RequestParam("skuNum") Integer skuNum,
                              Model model) {

        //远程调用添加购物车项
        Result<CartItem> cartItem = cartFeignClient.addSkuToCart(skuId, skuNum);
        if (cartItem.isOk()) {
            CartItem data = cartItem.getData();
            model.addAttribute("skuInfo", data);
            model.addAttribute("skuNum", data.getSkuNum());
        }

        return "cart/addCart";
    }
    //cart/deleteChecked
    @GetMapping("/cart/deleteChecked")
    public String deleteChecked(){
        //远程调用删除选中购物车项
        cartFeignClient.deleteChecked();
        return "cart/index";
    }
}
